﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;
using cecbClasses;
using cecbClasses.Data_Classes;
using cecbClasses.Data_Adapters;
using cecbClasses.Data_Adapters.Project;
using System.Data;

namespace CECB.Project
{
    /// <summary>
    /// Interaction logic for Insert_ProjEIADetails.xaml
    /// </summary>
    public partial class Insert_ProjEIADetails : Window
    {
        string destFile;
        string modlDestFile;
        private bool isTeamSelected = false;

        Func_Projects ObjProjctsFunc = new Func_Projects();
        Data_ProjectCriterias clsProjCrit = new Data_ProjectCriterias();
        Data_ImportCriterias clsProjCrit1 = new Data_ImportCriterias();


        public Insert_ProjEIADetails()
        {
            InitializeComponent();

            cecbClasses.Data_Adapters.Slider slid = new cecbClasses.Data_Adapters.Slider();

            TeamAutocompleteTextBox.ItemsSource = slid.getTeamDetails();
            comboBox_importnceCriteria.SelectedIndex = 0;
            comboBox_magnitudeCriteria.SelectedIndex = 0;
            comboBox_simpleCriteria.SelectedIndex = 0;
            comboBox_EIAMatrix.SelectedIndex = 0;
        }


        private bool ValidateData()
        {

            bool isValid = false;

            if (comboBox_ProjRefrnce.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select a Project", "CECB", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (comboBox_EIAMatrix.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select a Matrix", "CECB", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (((comboBox_importnceCriteria.SelectedIndex == 0) || ((comboBox_importnceCriteria.Text == ""))) && (wrapPanel_Importence.Visibility == System.Windows.Visibility.Visible))
            {
                MessageBox.Show("Please Select a Importance Criteria", "CECB", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (((comboBox_magnitudeCriteria.SelectedIndex == 0) || (comboBox_magnitudeCriteria.Text == "")) && (wrapPanel_Magnitude.Visibility == System.Windows.Visibility.Visible))
            {
                MessageBox.Show("Please Select a Magnitude Criteria", "CECB", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (((comboBox_simpleCriteria.SelectedIndex == 0) || (comboBox_simpleCriteria.Text == "")) && (wrapPanel_SimpleCiteria.Visibility == System.Windows.Visibility.Visible))
            {
                MessageBox.Show("Please Select a Simple Criteria", "CECB", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            else if (datePicker_FromDate.Text == "")
            {
                MessageBox.Show("Please Select a From Date", "CECB", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            else if (datePicker_ToDate.Text == "")
            {
                MessageBox.Show("Please Select a To Date", "CECB", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            else if (Convert.ToDateTime(datePicker_FromDate.Text) > Convert.ToDateTime(datePicker_ToDate.Text))
            {
                MessageBox.Show("Invalid Date Range", "CECB", MessageBoxButton.OK, MessageBoxImage.Error);

            }
            else if (TeamAutocompleteTextBox.Text == "")
            {
                MessageBox.Show("Please Select a Team", "CECB", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            //else if (checkBox_dicPending.IsChecked == false)
            //{
            //    MessageBox.Show("Please Select a Team", "CECB", MessageBoxButton.OK, MessageBoxImage.Error);

            //}
            else
            {
                isValid = true;
            }
            return isValid;
        }

        private void button_insertEIADetails_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ValidateData() == false)
                    return;


                Data_Projects clsProj = new Data_Projects();

                clsProj.Proj_reference = comboBox_ProjRefrnce.SelectedValue.ToString();
                clsProj.Proj_fromDate = Convert.ToDateTime(datePicker_FromDate.Text);
                clsProj.Proj_toDate = Convert.ToDateTime(datePicker_ToDate.Text);
                clsProj.Proj_finalDecision = "pending";
                clsProj.Proj_team = ((Teams)(TeamAutocompleteTextBox).SelectedItem).team_reference;




                ObjProjctsFunc.InsertEIADetails(clsProj);

                clsProjCrit.Proj_reference = comboBox_ProjRefrnce.SelectedValue.ToString();
                clsProjCrit.Proj_matrix = comboBox_EIAMatrix.Text;


                if (clsProjCrit.Proj_matrix == "Simple")
                {
                    ObjProjctsFunc.InsertMetrixCriterias(clsProjCrit);
                }
                else
                {
                    ObjProjctsFunc.InsertMetrixCriterias(clsProjCrit);

                    clsProjCrit1.Proj_reference = comboBox_ProjRefrnce.SelectedValue.ToString();
                    clsProjCrit1.Proj_matrix = comboBox_EIAMatrix.Text;

                    ObjProjctsFunc.InsertImportenceCriterias(clsProjCrit1);
                }


                int Days = datePicker_ToDate.SelectedDate.Value.Subtract(datePicker_FromDate.SelectedDate.Value).Days;

                if (clsProj.Proj_finalDecision != null && Days > 0)
                {
                    MessageBox.Show("Project EIA Details inserted successfully!", "CECB", MessageBoxButton.OK, MessageBoxImage.Information);
                    // SaveButton.IsEnabled = false;




                    comboBox_ProjRefrnce.SelectedIndex = 0;
                    textBox_projname.Text = "";
                    textBox_ProjType.Text = "";
                    textBox_Location.Text = "";

                    //image_PlanImage.
                    tbMultiLine.Text = "";
                    // comboBox_EIAMatrix.SelectedItem = "";
                    // comboBox_magnitudeCriteria.SelectedItem = "";
                    // comboBox_importnceCriteria.SelectedItem = "";
                    // comboBox_simpleCriteria.SelectedItem = "";
                    //datePicker_FromDate.Text = "";
                    //datePicker_ToDate.Text = "";
                    //checkBox_dicPending.IsChecked = false;

                    listView1.Items.Clear();
                    comboBox_importnceCriteria.SelectedIndex = 0;
                    comboBox_magnitudeCriteria.SelectedIndex = 0;
                    comboBox_simpleCriteria.SelectedIndex = 0;
                    comboBox_EIAMatrix.SelectedIndex = 0;
                    label_Dates.Content = "Duration";
                    //TeamAutocompleteTextBox.Text = "";
                    //datePicker_FromDate.Text = "Select a Date";
                    //datePicker_ToDate.Text = "Select a Date";

                    //}
                    //else
                    //{
                    //    MessageBox.Show("Project insertion is faillled!", "CECB", MessageBoxButton.OK, MessageBoxImage.Warning);
                    //}
                }
                //else
                //{
                //    MessageBox.Show("Check the box as pending");
                //}


            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                MessageBox.Show("Project insertion is faillled!", "CECB", MessageBoxButton.OK, MessageBoxImage.Warning);
                // SaveButton.IsEnabled = false;
            }

        }


        private void updateProjectTeam()
        {
            String prjref = comboBox_ProjRefrnce.SelectedValue.ToString();
            Projects prj = new DSSEntities().Projects.First(i => i.proj_reference == prjref);
            //  prj.proj = TeamAutocompleteTextBox.Text;

        }


        private void comboBox_magnitudeCriteria_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            //clsProjCrit.Proj_reference = comboBox_ProjRefrnce.Text;
            //clsProjCrit.Proj_matrix = comboBox_EIAMatrix.Text;
            clsProjCrit.Crt_type = "Magnitude";

            ComboBoxItem typeItem = (ComboBoxItem)comboBox_magnitudeCriteria.SelectedItem;
            string value = typeItem.Content.ToString();
            clsProjCrit.Crt_range = value;


        }


        private void comboBox_importnceCriteria_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {


            //clsProjCrit1.Proj_reference = comboBox_ProjRefrnce.Text;
            //clsProjCrit1.Proj_matrix = comboBox_EIAMatrix.Text;
            clsProjCrit1.Crt_type = "Importence";

            ComboBoxItem typeItem = (ComboBoxItem)comboBox_importnceCriteria.SelectedItem;
            string value = typeItem.Content.ToString();
            clsProjCrit1.Crt_range = value;



        }


        private void comboBox_simpleCriteria_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {


            clsProjCrit.Crt_type = "Simple";

            ComboBoxItem typeItem = (ComboBoxItem)comboBox_simpleCriteria.SelectedItem;
            string value = typeItem.Content.ToString();
            clsProjCrit.Crt_range = value;

        }




        public void FillProjectRefernces()
        {

            List<Projects> ProjRefrncesList = ObjProjctsFunc.GetAllRegProjRefernces();



            comboBox_ProjRefrnce.ItemsSource = ProjRefrncesList;
        }




        private void comboBox_ProjRefrnce_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                Data_Projects ObjProj = new Data_Projects();
                ObjProj.Proj_reference = comboBox_ProjRefrnce.SelectedValue.ToString();
                Projects selectedPrj = ObjProjctsFunc.GetProjectByCode(ObjProj);

                Data_ProjectTypes ObjProjType = new Data_ProjectTypes();
                ObjProjType.Ptype_reference = selectedPrj.proj_type;
                cecbData.ProjectTypes selectProjctType = ObjProjctsFunc.GetProjectByTypeCode(ObjProjType);



                if (selectedPrj.proj_reference != "-1")
                {
                    textBox_projname.Text = selectedPrj.proj_name;
                    textBox_ProjType.Text = selectProjctType.ptype_name;
                    textBox_Location.Text = selectedPrj.proj_location;
                    tbMultiLine.Text = selectedPrj.proj_otherDetails;

                    ShowData(comboBox_ProjRefrnce.SelectedValue.ToString());

                    destFile = selectedPrj.proj_planImage;
                    //image_PlanImage.Source = new BitmapImage(new Uri(destFile));


                    modlDestFile = selectedPrj.proj_modelImage;
                    // image_modelImage.Source = new BitmapImage(new Uri(modlDestFile));


                    if ((destFile == null) && (modlDestFile == null))
                    {
                        label_ModelInfo.Content = "Image not found".ToString();
                        label_PlanInfo.Content = "Image not found".ToString();
                        image_modelImage.Visibility = System.Windows.Visibility.Hidden;
                        image_PlanImage.Visibility = System.Windows.Visibility.Hidden;


                    }
                    else if (modlDestFile == null)
                    {

                        label_ModelInfo.Content = "Image not Found".ToString();
                        image_modelImage.Visibility = System.Windows.Visibility.Hidden;
                        label_PlanInfo.Content = "".ToString();
                        image_PlanImage.Visibility = System.Windows.Visibility.Visible;
                        image_PlanImage.Source = new BitmapImage(new Uri(destFile));

                    }
                    else if (destFile == null)
                    {
                        label_PlanInfo.Content = "Image not found".ToString();
                        image_PlanImage.Visibility = System.Windows.Visibility.Hidden;
                        label_ModelInfo.Content = "".ToString();
                        image_modelImage.Visibility = System.Windows.Visibility.Visible;
                        image_modelImage.Source = new BitmapImage(new Uri(modlDestFile));


                    }
                    else
                    {
                        label_ModelInfo.Content = "".ToString();
                        label_PlanInfo.Content = "".ToString();
                        image_PlanImage.Visibility = System.Windows.Visibility.Visible;
                        image_modelImage.Visibility = System.Windows.Visibility.Visible;
                        // destFile = selectedPrj.proj_planImage;
                        image_PlanImage.Source = new BitmapImage(new Uri(destFile));
                        // modlDestFile = selectedPrj.proj_modelImage;
                        image_modelImage.Source = new BitmapImage(new Uri(modlDestFile));

                    }

                }
            }
            catch
            {

                MessageBox.Show("Error");

            }
            updateProjectTeam();
        }




        private void button_Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void comboBox_EIAMatrix_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxItem currentItem = ((System.Windows.Controls.ComboBoxItem)comboBox_EIAMatrix.SelectedItem);

            if (currentItem.Content.Equals("Leopold"))
            {

                wrapPanel_SimpleCiteria.Visibility = System.Windows.Visibility.Hidden;
                wrapPanel_Magnitude.Visibility = System.Windows.Visibility.Visible;
                wrapPanel_Importence.Visibility = System.Windows.Visibility.Visible;
            }
            else if (currentItem.Content.Equals("Simple"))
            {
                wrapPanel_Magnitude.Visibility = System.Windows.Visibility.Hidden;
                wrapPanel_Importence.Visibility = System.Windows.Visibility.Hidden;

                wrapPanel_SimpleCiteria.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                wrapPanel_SimpleCiteria.Visibility = System.Windows.Visibility.Hidden;
                wrapPanel_Magnitude.Visibility = System.Windows.Visibility.Hidden;
                wrapPanel_Importence.Visibility = System.Windows.Visibility.Hidden;
            }


        }




        private void datePicker_ToDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            int Days = datePicker_ToDate.SelectedDate.Value.Subtract(datePicker_FromDate.SelectedDate.Value).Days;

            if (datePicker_ToDate.SelectedDate.HasValue && Days > 0)
                label_Dates.Content = "No Of Days : " + Days.ToString();

            else
                label_Dates.Content = "Entered Time Duration is Inavlied!!!";
        }

        private void datePicker_FromDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            FillProjectRefernces();
            wrapPanel_SimpleCiteria.Visibility = System.Windows.Visibility.Hidden;
            wrapPanel_Magnitude.Visibility = System.Windows.Visibility.Hidden;
            wrapPanel_Importence.Visibility = System.Windows.Visibility.Hidden;


        }

        private void expander_locationDetails_Expanded(object sender, RoutedEventArgs e)
        {

        }


        private void ShowData(string gridProjref)
        {

            List<Project_AreaGrids> ListPrjGridDtls = ObjProjctsFunc.GetGridDtlsByRef(gridProjref);

            listView1.Items.Clear();
            foreach (Project_AreaGrids oPrjTypeDtls in ListPrjGridDtls)
            {
                Data_ProjectGridLocation oPrjTypes = new Data_ProjectGridLocation();
                oPrjTypes.Loc_latitude = oPrjTypeDtls.loc_latitude.ToString();
                oPrjTypes.Loc_longitude = oPrjTypeDtls.loc_longitude.ToString();

                listView1.Items.Add(oPrjTypes);

            }


        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {
            // SaveButton.IsEnabled = true;
        }

        private void TeamReferenceTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            isTeamSelected = true;
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void button_catAdd_Click(object sender, RoutedEventArgs e)
        {
            new Team_Window().ShowDialog();
            cecbClasses.Data_Adapters.Slider slid = new cecbClasses.Data_Adapters.Slider();

            TeamAutocompleteTextBox.ItemsSource = slid.getTeamDetails();
        }

        private void datePicker_Todatete_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {

        }


    }
}
